package com.hl.toolkit.server.tycoin.system.slaverservice;

import com.tycoin.common.repository.AbstractRepository;
import com.tycoin.common.service.AbstractService;
import com.tycoin.system.slaveentity.User;
import com.tycoin.system.slaverepository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional(readOnly = true)
public class UserService extends AbstractService<User, Long> {

    @Autowired
    private UserRepository userRepository;

    @Override
    public AbstractRepository<User, Long> getRepository() {
        return userRepository;
    }

    @Transactional(readOnly = false)
    public void updateUserLoginInfo(Long id, String ip) {
        User user = userRepository.findOne(id);
        user.setLastLoginIp(user.getLoginIp());
        user.setLastLoginTime(user.getLoginTime());

        user.setLoginIp(ip);
        user.setLoginTime(new Date());
        if (user.getLoginNum() != null && user.getLoginNum() >= 0) {
            user.setLoginNum(user.getLoginNum() + 1);
        } else {
            user.setLoginNum(1);
        }
        userRepository.save(user);
    }
}
